<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\index\model\Set;
use app\index\model\Game;
use app\index\model\Pool;
use app\index\model\User;
use app\index\model\Log;
use app\index\model\Box;

use think\Request;
class Tb extends Base{
	/**
	 * 获取首页信息
	 *
	 * @return void
	 * @author 
	 **/
	public function index()
	{
		$shop_id=Request::instance()->param('shop_id');
		$set=Set::get(['shop_id'=>$shop_id]);
		$person_num=Box::where('shop_id',$shop_id)->group('open_id')->count();//获取参与人数
		$max_limit=$set->max_limit;
		$percent=round(100*$person_num/$max_limit);//参与人数百分比
		$start=strtotime($set->start);//开始日期转换为时间戳
		$end=strtotime($set->end);//结束日期转换为时间戳
		if(time()<$start){
			$status=0;
			$timeStamp=$start-time();
		}elseif(time()>$start&&time()<$end){
			$status=1;
			$timeStamp=$end-time();
		}else{
			$status=2;
			$timeStamp=0;
		}
		$pool=Pool::get(['shop_id'=>$shop_id]);
		$game=Game::get(['shop_id'=>$shop_id]);
		$set['person_num']=$person_num;
		$set['percent']=$percent;
		$set['status']=$status;
		$set['timeStamp']=$timeStamp;
		$set['personLimit']=$game->personLimit;
		$set['dayLimit']=$game->dayLimit;
		$set['isFavor']=$game->isFavor;
		$set['pool']=$pool;
		$this->success($set);

	}
	/**
	 * 增加页面浏览次数
	 *
	 * @return void
	 * @author 
	 **/
	public function addPageView()
	{
		$shop_id=Request::instance()->param('shop_id');
		$set=Set::get(['shop_id'=>$shop_id]);
		$set->page_view+=1;
		$set->allowField(true)->save();
		$this->success();

	}
	/**
	 * 用户注册
	 *
	 * @return void
	 * @author 
	 **/
	function addUser()
	{
		$open_id=Request::instance()->param('open_id');
		$shop_id=Request::instance()->param('shop_id');
		$data=Request::instance()->param();
		if(User::get(['shop_id'=>$shop_id,'open_id'=>$open_id])==null){
			$user=new User($data);
		$user->allowField(true)->save();
		}
		$this->success();
	}
	/**
	 * 领取礼盒
	 *
	 * @return void
	 * @author 
	 **/
	public function getBox()
	{
		$shop_id=Request::instance()->param('shop_id');
		$open_id=Request::instance()->param('open_id');
		$user_nick=Request::instance()->param('user_nick');
		$time=date('Y-m-d',time());
		$isFavor=Game::get(['shop_id'=>$shop_id])->isFavor;//未收藏店铺是否可参与，0是1否
		$personLimit=Game::get(['shop_id'=>$shop_id])->personLimit;//每人领取限额
		$dayLimit=Game::get(['shop_id'=>$shop_id])->dayLimit;//每日领取限额
		$personNum=Box::where(['shop_id'=>$shop_id,'open_id'=>$open_id])->count();//本人已领取数量
		$dayNum=Box::where(['shop_id'=>$shop_id,'create_time'=>$time])->count();//当日已领取数量
		$userNum=Box::where(['shop_id'=>$shop_id])->group('open_id')->count();//已参与人数
		$max_limit=Set::get(['shop_id'=>$shop_id])->max_limit;
		if($isFavor==1){
			$this->success('请先收藏店铺再来领取！');
		}
		elseif($personNum<$personLimit&&$dayNum<$dayLimit&&$userNum<$max_limit){
			$box=new Box;
			$box->shop_id=$shop_id;
			$box->open_id=$open_id;
			$box->user_nick=$user_nick;
			$box->status=0;
			$box->create_time=$time;
			$box->save();
			$this->success('领取成功！');
		}elseif($personNum>=$personLimit){
			$this->success('您已达到领取上限!');
		}elseif($dayNum>=$dayLimit){
			$this->success('今日礼盒领取已达上限，请明日再来!');
		}else{
			$this->success('活动参与人数已达上限，请关注下次活动！');
		}
		
		
	}
	/**
	 * 拆礼盒增加拆礼盒记录
	 *
	 * @return void
	 * @author 
	 **/
	function addLog()
	{
		$data=Request::instance()->param();
		$shop_id=Request::instance()->param('shop_id');
		$main_id=Request::instance()->param('main_id');
		$box_id=Request::instance()->param('box_id');
		$log=new Log($data);
		$log->allowField(true)->save();//保存帮拆记录
		$user=User::get(['shop_id'=>$shop_id,'open_id'=>$main_id]);
		$user->follower+=1;
		$user->save();
		$game=Game::get(['shop_id'=>$shop_id]);
		$shareNumber=$game->shareNumber;
		$logNum=count(Log::get(['box_id'=>$box_id]));
		if($logNum==$shareNumber){
			$box=Box::get($box_id);
			$box->status=1;
			$box->save();
		}

		$this->success();

	}
	/**
	 * 获取礼盒进度
	 *
	 * @return void
	 * @author 
	 **/
	public function my()
	{
		$open_id=Request::instance()->param('open_id');
		$shop_id=Request::instance()->param('shop_id');
		$box=Box::all(['shop_id'=>$shop_id,'open_id'=>$open_id]);
		$game=Game::get(['shop_id'=>$shop_id]);//获取游戏设置
		$shareNumber=$game->shareNumber;//获取游戏规则中的分享次数
		$boxArr=[];
		foreach($box as $k=>$v){
			$number1=Log::where(['shop_id'=>$shop_id,'open_id'=>$open_id,'box_id'=>$v->id])->count();//获取拆盒子的人数
		if($number1<$shareNumber){
			$number2=$shareNumber-$number1;//还需几个人拆盒子
			$percent=round(100*$number1/$shareNumber);//拆盒子进度
			// $v->number1=$number1;
			// $v->number2=$number2;
			// $v->percent=$percent;
			// $v->status=0;
			$boxArr[]=[
				'id'=>$v->id,
				'number1'=>$number1,
				'number2'=>$number2,
				'percent'=>$percent,
				'status'=>0
			];

		}else{
			$boxArr[]=[
				'id'=>$v->id,
				'number1'=>$number1,
				'number2'=>0,
				'percent'=>100,
				'status'=>1
			];
		}
		}
		
		
		$this->success($boxArr);

	}
	/**
	 * 获取排行榜
	 *
	 * @return void
	 * @author 
	 **/
	function getRank()
	{
		$open_id=Request::instance()->param('open_id');
		$shop_id=Request::instance()->param('shop_id');
		$user=User::where(['shop_id'=>$shop_id])->order('follower desc')->select();
		$this->success($user);


	}
	/**
	 * 获取中奖结果
	 *
	 * @return void
	 * @author 
	 **/
	public function getBox2()
	{
		$shop_id=Request::instance()->param('shop_id');
		$box_id=Request::instance()->param('box_id');
		$box=Box::get($box_id);
		$bonus=$box->bonus;//中奖结果


		if($bonus==1){
			$data=[
				'title'=>'一等奖',
				'name'=>Pool::get(['shop_id'=>$shop_id])->name1,
				'type'=>Pool::get(['shop_id'=>$shop_id])->type1,
				'activity_url'=>Pool::get(['shop_id'=>$shop_id])->activity_url1,
			];
			$this->success($data);
		}
		elseif($bonus==2){
			$data=[
				'title'=>'二等奖',
				'name'=>Pool::get(['shop_id'=>$shop_id])->name2,
				'type'=>Pool::get(['shop_id'=>$shop_id])->type2,
				'activity_url'=>Pool::get(['shop_id'=>$shop_id])->activity_url2,
			];
			$this->success($data);
		}
		elseif($bonus==3){
			$data=[
				'title'=>'三等奖',
				'name'=>Pool::get(['shop_id'=>$shop_id])->name3,
				'type'=>Pool::get(['shop_id'=>$shop_id])->type3,
				'activity_url'=>Pool::get(['shop_id'=>$shop_id])->activity_url3,
			];
			$this->success($data);
		}else{
			$pool=Pool::get(['shop_id'=>$shop_id]);
		$number1=$pool->number1;//一等奖奖池数量
		$number2=$pool->number2;//二等奖奖池数量
		$number3=$pool->number3;//三等奖奖池数量
		$number11=count(Box::all(['shop_id'=>$shop_id,'bonus'=>1]));//一等奖领取数量
		$number21=count(Box::all(['shop_id'=>$shop_id,'bonus'=>2]));//二等奖领取数量
		$number31=count(Box::all(['shop_id'=>$shop_id,'bonus'=>3]));//三等奖领取数量
		$number12=$number1-$number11;//一等奖剩余数量
		$number22=$number2-$number21;//二等奖剩余数量
		$number32=$number3-$number31;//三等奖剩余数量
		$bonus=$this->get_rand([$number12,$number22,$number32]);
		$box=Box::get($box_id);
		$box->bonus=$bonus;
		$box->save();
		$this->getBox2();
		}
	}	
		
	/**
	 * 提交发货地址
	 *
	 * @return void
	 * @author 
	 **/
	function address()
	{
		$open_id=Request::instance()->param('open_id');
		$shop_id=Request::instance()->param('shop_id');
		$box_id=Request::instance()->param('box_id');
		$address=Request::instance()->param('name').Request::instance()->param('mobile').Request::instance()->param('address');
		$box=Box::get($box_id);
		$box->save(['address'=>$address]);

	}
	//中奖算法
	public function get_rand($proArr) {
    $result = '';
 
    //概率数组的总概率精度
    $proSum = array_sum($proArr);
 
    //概率数组循环
    foreach ($proArr as $key => $proCur) {
        $randNum = mt_rand(1, $proSum);
        if ($randNum <= $proCur) {
            $result = $key;
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);
 
    return $result;
}
	

	

}